package com.baosight.scaq.aq.ci.service;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.scaq.aq.ci.domain.AQCIPenalty;
import com.baosight.scaq.common.aq.basetools.StaticTools;
import com.baosight.scaq.common.aq.cInterface.Department;
/**
 * 罚款信息表
 */
public class ServiceAQCI42 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		//获取登录人信息
		String miningcode = (String) UserSession.getInSessionProperty("miningCode");//c.102
//		String fdName =(String) UserSession.getInSessionProperty("miningName");//矿名称
//		if(fdName!=null&&!"".equals(fdName))
//		{
//		if(fdName.indexOf("打通一矿") > 0 ){
//			fdName = "打通一矿";
//		}
//		else if(fdName.indexOf("松藻煤矿") > 0 ){
//			fdName = "松藻煤矿";
//		}
//		else if(fdName.indexOf("渝阳煤矿") > 0 ){
//			fdName = "渝阳煤矿";
//		}
//		else if(fdName.indexOf("石壕煤矿") > 0 ){
//			fdName = "石壕煤矿";
//		}
//		else if(fdName.indexOf("逢春煤矿") > 0 ){
//			fdName = "逢春煤矿";
//		}
//		}
		//if(miningcode!="C.102"){
		//获取矿名称过滤信息
		//对打通一矿做处理 -- 松藻煤电公司打通一矿
		//if(fdName.length()>4) fdName = fdName.substring(6);
		//作为参数封装进inInfo中
//		StaticTools.setQueryParameters(inInfo, "fdName", fdName, true);
		//}		
		//获取页面日期
		String querydate = inInfo.getString("querydate");
		//判断获取是否为空 -- 为空
		if(querydate==null || querydate.trim().equals("")){
		//赋值为当前日期
			querydate = StaticTools.getDate();
			inInfo.set("querydate", querydate);
		}
		//获取矿名称并转码
				String mineId = inInfo.getString("fdMiningId");
				if(mineId != null && !"".equals(mineId)){
				try {
					mineId=URLDecoder.decode(mineId,"utf8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				}
		
		//有值直接加入EiInfo中传入
		StaticTools.setQueryParameters(inInfo, "fdVirtualDate", querydate, true);
		StaticTools.setQueryParameters(inInfo, "fdName", mineId, false);
		EiInfo outInfo = super.query(inInfo, "AQCIPenalty.query", new AQCIPenalty());
		return outInfo;
	}	
	
	/**
	 * 矿井block
	 * @return
	 */
	private EiBlock getMines() {
		// TODO Auto-generated method stub
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("AQCIGasInfoNew.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}
	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outinfo = new EiInfo();
		AQCIPenalty _dom = new AQCIPenalty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.insert("AQCIPenalty.insert", _dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		AQCIPenalty _dom = new AQCIPenalty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.update("AQCIPenalty.update", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		AQCIPenalty _dom = new AQCIPenalty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("AQCIPenalty.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	} 
} 